$(function () {

    getOneDay();
    getTimes();
});


function getOneDay(){
     laydate.render({
        elem: '#oneDay' ,//指定元素
        type: 'date',
         value:new Date(),
         trigger:'click',
        max:0
        ,done: function(oneDay, date, endDate){
            $('#hidden').val(oneDay)
         }
    });

}


function  oneDayStat(){
    var oneDay=$('#hidden').val();
        // alert(oneDay)
    $.ajax({
        url:"statistics/oneDay",
        type:"get",
        dataType:"json",
        data:{
            oneDay:oneDay
        },
        success:function(data){
            var movies= data.data;
            console.log(movies)
            initData(movies);
        }
    });
}

function getTimes() {
    //日期时间范围选择
    laydate.render({
        elem: '#times'
        ,type: 'date'
        ,range: true //或 range: '~' 来自定义分割字符
        ,done: function(value, date, endDate){
            // $('#hidden').val(oneDay)
            alert(date)
            alert(endDate)
        }
    });
}



function listTimes() {
    var  start_time=$('start_time').val();
    var  end_time=$('end_time').val();

    $.ajax({
        url:"/statistics/times",
        type:"get",
        dataType:"json",
        data:{
            start_time:start_time,
            end_time:end_time
        },
        success:function(data){
            var movies= data.data;
            console.log(movies)
            // initData(movies);
        }
    })
}



//生成数据
function initData(movies) {
    // console.log(movies)
    var legendData = ['观影人数', '票房'];
    var axisLabel = [];
    for (var a=0;a<movies.length;a++){
        axisLabel.push(movies[a].movie_name);
    }
    var bgColorList = ['#FF1493', '#1a37f2'];

    var seriesValue = [];
    //电影循序
    for(var i =0;i<legendData.length;i++){
        var arrData = [];
        var seriesDataVal = null;

        if(i<1){
            for (var j = 0; j < axisLabel.length; j++) {
                arrData.push(movies[j].quantity);
            }
        }
        else {
            for (var j = 0; j < axisLabel.length; j++) {
                arrData.push(movies[j].totalPrice)
            }
        }

        seriesDataVal = {
            barWidth: 10,//柱状条宽度
            name:legendData[i],
            type:'bar',
            itemStyle: {
                normal: {
                    show: true,//鼠标悬停时显示label数据
                    barBorderRadius: [10, 10, 10, 10],//柱形图圆角，初始化效果
                    color: bgColorList[i]
                }
            },
            label: {
                normal: {
                    show: true, //显示数据
                    position: 'top'//显示数据位置 'top/right/left/insideLeft/insideRight/insideTop/insideBottom'
                }
            } ,
            data:arrData
        };
        seriesValue.push(seriesDataVal);
    }

    buildChart(legendData, axisLabel, seriesValue);

}

//生成Echarts图形
function buildChart(legendData, axisLabel, seriesValue) {
    var chart = document.getElementById('barsDemo');
    var echart = echarts.init(chart);
    var option = {
        title: {
            text: "每日电影观影人数和票房统计",//正标题
            x: "center", //标题水平方向位置
            y: "0", //标题竖直方向位置
            textStyle: {
                fontSize: 18,
                fontWeight: 'normal',
                color: '#666'
            }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'//阴影，若需要为直线，则值为'line'
            }
        },
        toolbox: {
            show: true,
            feature: {
                saveAsImage: {show: true}
            }
        },
        legend: {
            data: legendData,
            y: 'bottom'//图例说明文字设置

        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '10%',
            top:'10%',
            containLabel: true
        },
        xAxis: [{
            min: 0,
            type: 'category', //纵向柱状图，若需要为横向，则此处值为'value'， 下面 yAxis 的type值为'category'
            data: axisLabel
        }],
        yAxis: [{
            min: 0,
            type: 'value',
            splitArea: {show: false}
        }],
        label: {
            normal: { //显示bar数据
                show: true,
                position: 'top'
            }
        },
        series: seriesValue
    };

    echart.setOption(option);
}



